/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Br.Com.Cds.Academia.View;


import Br.Com.Cds.Academia.DAO.AcessoMySQL;
import java.awt.BorderLayout;  
import java.awt.Component;  
import java.awt.Graphics;  
import java.awt.Image;  
import java.awt.Panel;  
import java.awt.image.RenderedImage;  
import java.io.File;  
import java.io.IOException;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import javax.imageio.ImageIO;  
import javax.media.Buffer;  
import javax.media.CaptureDeviceInfo;  
import javax.media.Manager;  
import javax.media.MediaLocator;  
import javax.media.Player;  
import javax.media.cdm.CaptureDeviceManager;  
import javax.media.control.FrameGrabbingControl;  
import javax.media.format.VideoFormat;  
import javax.media.util.BufferToImage;  
import javax.swing.JButton;  
import javax.swing.JOptionPane;  
import javax.swing.JPanel;  
  
  
public class CapturaFoto extends JPanel {  
      
      
    public static Player player = null;  
    public CaptureDeviceInfo di = null;  //  @jve:decl-index=0:  
    public MediaLocator ml = null;  //  @jve:decl-index=0:  
    public JButton capture = null;  
    public Buffer buf = null;  
    public Image img = null;  
    public VideoFormat vf = null;  
    public BufferToImage btoi = null;  
    public ImagePanel imgpanel = null;  
  
    private static final long serialVersionUID = 1L;  
    private JButton captura = null;  
  
  
    public CapturaFoto() {  
        super();  
        initialize();  
    }  
  
    public static void main(String [] args){
        
    }
  
    private void initialize() {  
        this.setLayout(new BorderLayout());  
        this.setSize(327, 259);  
        this.add(getCaptura(), BorderLayout.SOUTH);  
        this.setVisible(true);  
          
  
        String str2 = "vfw//0";  
        di = CaptureDeviceManager.getDevice(str2);  
        ml =  new MediaLocator("vfw://0");  
        try {  
            player = Manager.createRealizedPlayer(ml);  
            player.start();  
            Component comp;  
            if ((comp = player.getVisualComponent()) != null) {  
                add(comp, BorderLayout.NORTH);  
            }  
            add(captura, BorderLayout.SOUTH);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
      
   }  
    
    
  
    public void gravaImg (Image imagem){    
        
        PreparedStatement pstm;
        ResultSet rs;
        int maxId = 0;
        
        try{
             AcessoMySQL mysql = new AcessoMySQL();
             pstm = (PreparedStatement) mysql.conectar().prepareStatement("SELECT MAX(IDALUNO) FROM ALUNO");
             rs = pstm.executeQuery();
             maxId =   rs.getInt("IDALUNO")+1;
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "ERRO AO TENTAR CAPTURAR!","CDS ACADEMIA",JOptionPane.ERROR_MESSAGE);
        }
        String ID = String.valueOf(maxId);
        String caminho = "C:\\Fotos"+"00"+ID+".JPG";  
        try {  
           ImageIO.write((RenderedImage) imagem, "jpg", new File(caminho));  
            JOptionPane.showMessageDialog(this, "Imagem Capturada!");  
        } catch (IOException e) {  
            JOptionPane.showMessageDialog(null, "não foi possivel encontrar " +  
                   "o dispositivo para a captura da imagem.");  
            e.printStackTrace();  
        }  
          
    }  
    private JButton getCaptura() {  
        if (captura == null) {  
            captura = new JButton("Captura");  
            captura.setBounds(50, 50, 50, 50);  
            captura.addActionListener(new java.awt.event.ActionListener() {  
                public void actionPerformed(java.awt.event.ActionEvent e) {  
                    FrameGrabbingControl fgc = (FrameGrabbingControl) player  
                            .getControl("javax.media.control.FrameGrabbingControl");  
                    buf = fgc.grabFrame();  
                    btoi = new BufferToImage((VideoFormat) buf.getFormat());  
                    img = btoi.createImage(buf);  
                    gravaImg(img);  
                }  
            });  
        }  
        return captura;  
    }  
      
    class ImagePanel extends Panel {  
  
        private static final long serialVersionUID = 1L;  
        public Image myimg = null;  
  
        public ImagePanel() {  
            JOptionPane.showMessageDialog(null, "setando a classe");  
            setLayout(null);  
            setSize(320, 240);  
        }  
  
        public void setImage(Image img) {  
            this.myimg = img;  
           repaint();  
        }  
  
        public void paint(Graphics g) {  
            if (myimg != null) {  
                g.drawImage(myimg, 0, 0, this);  
            }  
        }  
  
    }  
  
}  

